package com.itheima.service.impl;

import com.itheima.VO.*;
import com.itheima.entity.SysConfig;
import com.itheima.mapper.ConfigMapper;
import com.itheima.service.SysConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class SysConfigServiceImpl implements SysConfigService {

    private final ConfigMapper configMapper;

    @Override
    public ConfigAllVO selectAllConfigData() {
        ConfigAllVO configAllVO = new ConfigAllVO();
        List<ConfigListVO> list = new ArrayList<>();

        //1
        ConfigListVO configListVO1 = new ConfigListVO();
        ConfigListVO configListVO2 = new ConfigListVO();
        ConfigListVO configListVO3 = new ConfigListVO();
        configListVO1.setName("基础配置");
        configListVO1.setKey("base");
        configListVO2.setName("系统设置");
        configListVO2.setKey("system");
        configListVO3.setName("其他设置");
        configListVO3.setKey("custom");
        addconfigListVO(configListVO1,configListVO1.getKey());
        addconfigListVO(configListVO2,configListVO2.getKey());
        addconfigListVO(configListVO3,configListVO3.getKey());

        list.add(configListVO1);
        list.add(configListVO2);
        list.add(configListVO3);
        configAllVO.setList(list);

        //2
        List<ConfigVO> configVOList = new ArrayList<>();
        List<SysConfig> sysConfigList = configMapper.getList();
        for (SysConfig sysConfig : sysConfigList) {
            configVOList.add(sysConfigconConverConfigVO(sysConfig));
        }
        List<ConfigAllDataVO> configAllDataVOList = new ArrayList<>();
        ConfigAllDataVO configAllDataVO = new ConfigAllDataVO();
        configAllDataVO.setAllData(configVOList);
        configAllDataVOList.add(configAllDataVO);
        configAllVO.setAllData(configAllDataVOList);

        //3
        ConfigFormVO configFormVO = new ConfigFormVO();
        List<Map<String, String>> configFormVOs = configMapper.getAttrValue();
        Map<String, String> maps = new HashMap<>();
        for (Map<String, String> m : configFormVOs) {
            String key = m.get("attr_key");
            String value = m.get("attr_value");
            maps.put(key, value);
        }
        configFormVO.setSystem_name(maps.get("system_name"));
        configFormVO.setSystem_logo(maps.get("system_logo"));
        configFormVO.setWeb_ico(maps.get("web_ico"));
        configFormVO.setSite_name(maps.get("site_name"));
        configFormVO.setSite_link(maps.get("site_link"));
        configFormVO.setSite_keywords(maps.get("site_keywords"));
        configFormVO.setClose_website(maps.get("close_website"));
        configFormVO.setCopy_code(maps.get("copy_code"));
        configFormVO.setCopy_info(maps.get("copy_info"));
        configFormVO.setPhone(maps.get("phone"));
        configFormVO.setCopy_url(maps.get("copy_url"));
        configFormVO.setBlack_ip(maps.get("black_ip"));
        configFormVO.setLogin_title(maps.get("login_title"));
        configFormVO.setLogin_bg(maps.get("login_bg"));
        configFormVO.setNotic_content(maps.get("notic_content"));
        configFormVO.setLoginreg_bg(maps.get("loginreg_bg"));
        configFormVO.setWeb_img(maps.get("web_img"));
        configFormVO.setNotic_title(maps.get("notic_title"));
        configFormVO.setSite_description(maps.get("site_description"));
        configAllVO.setConfigForm(configFormVO);

        //4
        ConfigRulesVO configRulesVO = new ConfigRulesVO();
        configRulesVO.setSite_name(configMapper.getSiteName());
        configAllVO.setConfigRules(configRulesVO);

        return configAllVO;
    }

    @Override
    public void insertConfig(ConfigVO configVO) {
        configMapper.insertConfig(configVO);
    }


    private void addconfigListVO(ConfigListVO configListVO1, String x) {
        List<ChildrenVO> childrenVOList = new ArrayList<>();
        List<String> names = configMapper.getGroupValueByTabValue(x);
        for (String name : names) {
            ChildrenVO childrenVO = new ChildrenVO();
            childrenVO.setName(name);
            List<SysConfig> sysConfigList = configMapper.getByGroupValue(name);
            List<ConfigVO> configVOList = new ArrayList<>();
            for (SysConfig sysConfig : sysConfigList) {
                ConfigVO configVO = sysConfigconConverConfigVO(sysConfig);
                configVOList.add(configVO);
            }
            childrenVO.setData(configVOList);
            childrenVOList.add(childrenVO);
        }
        configListVO1.setChildren(childrenVOList);
    }


    private ConfigVO sysConfigconConverConfigVO(SysConfig sysConfig) {
        ConfigVO configVO = new ConfigVO();
        configVO.setId(sysConfig.getId());
        configVO.setGroup_value(sysConfig.getGroupValue());
        configVO.setTab_value(sysConfig.getTabValue());
        configVO.setAttr_key(sysConfig.getAttrKey());
        configVO.setAttr_name(sysConfig.getAttrName());
        configVO.setAttr_value(sysConfig.getAttrValue());
        configVO.setDel_flag(sysConfig.getDelFlag());
        configVO.setType(sysConfig.getType());
        configVO.setConfig_value(sysConfig.getConfigValue());
        configVO.setValidator(sysConfig.getValidator());
        configVO.setPlaceholder(sysConfig.getPlaceholder());
        configVO.setCan_delete(sysConfig.getCanDelete());
        configVO.setSort_num(sysConfig.getSortNum());
        return configVO;
    }
}
